package com.bh.fee.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.common.constant.Entity;
import com.bh.common.core.redis.RedisCache;
import com.bh.common.exception.ServiceException;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.SecurityUtils;
import com.bh.fee.domain.JcFeeType;
import com.bh.fee.mapper.JcFeeTypeMapper;
import com.bh.fee.service.IJcFeeTypeService;
import com.bh.kysys.domain.JcSysOrganization;
import com.bh.kysys.mapper.JcSysOrganizationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 费用类型Service业务层处理
 *
 * @author slx
 * @date 2023-05-21
 */
@Service
public class JcFeeTypeServiceImpl extends ServiceImpl<JcFeeTypeMapper, JcFeeType> implements IJcFeeTypeService {

    public static final String FEE_TYPE = "FEE_TYPE_OPTIONS:";
    public static final String TRANSPORT_ORDER = "1";
    public static final String TRANSPORT_SHIPMENT = "2";
    public static final String TRANSPORT_DISPATCH = "3";
    public static final String TRANSPORT_MIX_ORDER = "1.1";
    public static final String TRANSPORT_MIX_SHIPMENT = "2.1";
    public static final String TRANSPORT_MIX_DISPATCH = "3.1";

    @Autowired
    private JcFeeTypeMapper jcFeeTypeMapper;
    @Autowired
    private JcSysOrganizationMapper jcSysOrganizationMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询费用类型
     *
     * @param id 费用类型主键
     * @return 费用类型
     */
    @Override
    public JcFeeType selectJcFeeTypeById(Long id) {
        return jcFeeTypeMapper.selectById(id);
    }

    @Override
    public JcFeeType selectJcFeeTypeByCode(String code) {
        return jcFeeTypeMapper.selectJcFeeTypeByCode(code);
    }

    /**
     * 查询费用类型列表
     *
     * @param jcFeeType 费用类型
     * @return 费用类型
     */
    @Override
    public List<JcFeeType> selectJcFeeTypeList(JcFeeType jcFeeType) {
        return jcFeeTypeMapper.selectJcFeeTypeList(jcFeeType);
    }

    /**
     * 查询混合类型
     * @param type
     * @return
     */
    @Override
    public List<JcFeeType> listOption(String type) {
        List<JcFeeType> list = redisCache.getCacheObject(FEE_TYPE + type);
        if (list == null) {
            updateCacheFeeTypeListByType(type);
            list = redisCache.getCacheObject(FEE_TYPE + type);
        }
        return list;
    }

    /**
     * 查询所属类型
     * @param type
     * @return
     */
    @Override
    public List<JcFeeType> listModelOption(String type) {
        List<JcFeeType> list = redisCache.getCacheObject(FEE_TYPE + type + SecurityUtils.getOrgId());
        if (list == null) {
            updateCacheFeeTypeListByType(type);
            list = redisCache.getCacheObject(FEE_TYPE + type + SecurityUtils.getOrgId());
        }
        return list;
    }


    private void updateCacheFeeTypeListByType(String type) {
        if (TRANSPORT_ORDER.equals(type)) {
            JcFeeType jcFeeType = new JcFeeType();
            jcFeeType.setStatus(Entity.ACTIVE);
            jcFeeType.setTransportorder(1);
            jcFeeType.setCost(0);
            jcFeeType.setOrgId(SecurityUtils.getOrgId());
            List<JcFeeType> list = jcFeeTypeMapper.selectJcFeeTypeList(jcFeeType);
            redisCache.setCacheObject(FEE_TYPE + TRANSPORT_ORDER + SecurityUtils.getOrgId(), list);
        }

        if (TRANSPORT_MIX_ORDER.equals(type)) {
            JcFeeType jcFeeType = new JcFeeType();
            jcFeeType.setStatus(Entity.ACTIVE);
            jcFeeType.setTransportorder(1);
            jcFeeType.setCost(0);
            List<JcFeeType> list = jcFeeTypeMapper.selectJcFeeTypeList(jcFeeType);
            redisCache.setCacheObject(FEE_TYPE + TRANSPORT_MIX_ORDER, list);
        }

        if (TRANSPORT_SHIPMENT.equals(type)) {
            JcFeeType jcFeeType = new JcFeeType();
            jcFeeType.setStatus(Entity.ACTIVE);
            jcFeeType.setShipmentModule(1);
            jcFeeType.setCost(1);
            jcFeeType.setOrgId(SecurityUtils.getOrgId());
            List<JcFeeType> list = jcFeeTypeMapper.selectJcFeeTypeList(jcFeeType);
            redisCache.setCacheObject(FEE_TYPE + TRANSPORT_SHIPMENT + SecurityUtils.getOrgId(), list);
        }

        if (TRANSPORT_MIX_SHIPMENT.equals(type)) {
            JcFeeType jcFeeType = new JcFeeType();
            jcFeeType.setStatus(Entity.ACTIVE);
            jcFeeType.setShipmentModule(1);
            jcFeeType.setCost(1);
            List<JcFeeType> list = jcFeeTypeMapper.selectJcFeeTypeList(jcFeeType);
            redisCache.setCacheObject(FEE_TYPE + TRANSPORT_MIX_SHIPMENT, list);
        }

        if (TRANSPORT_DISPATCH.equals(type)) {
            JcFeeType jcFeeType = new JcFeeType();
            jcFeeType.setStatus(Entity.ACTIVE);
            jcFeeType.setDispatch(1);
            jcFeeType.setCost(1);
            jcFeeType.setOrgId(SecurityUtils.getOrgId());
            List<JcFeeType> list = jcFeeTypeMapper.selectJcFeeTypeList(jcFeeType);
            redisCache.setCacheObject(FEE_TYPE + TRANSPORT_DISPATCH + SecurityUtils.getOrgId(), list);
        }
    }

    /**
     * 新增费用类型
     *
     * @param jcFeeType 费用类型
     * @return 结果
     */
    @Override
    public int insertJcFeeType(JcFeeType jcFeeType) {
        // 判断code是否重复
        JcFeeType jcFeeType1 = jcFeeTypeMapper.selectJcFeeTypeByCode(jcFeeType.getCode());
        if (jcFeeType1 != null) {
            throw new ServiceException("费用类型编码重复");
        }
        JcSysOrganization sysOrganization = jcSysOrganizationMapper.selectById(jcFeeType.getOrgId());
        if (sysOrganization != null) {
            jcFeeType.setOrgName(sysOrganization.getName());
        }
        jcFeeType.setCreateName(SecurityUtils.getUsername());
        jcFeeType.setCreateTime(DateUtils.getNowDate());
        return jcFeeTypeMapper.insert(jcFeeType);
    }

    /**
     * 修改费用类型
     *
     * @param jcFeeType 费用类型
     * @return 结果
     */
    @Override
    public int updateJcFeeType(JcFeeType jcFeeType) {
        // 更新的时候判断code是否重复
        JcFeeType jcFeeType1 = jcFeeTypeMapper.selectJcFeeTypeByCode(jcFeeType.getCode());
        if (jcFeeType1 != null && !jcFeeType1.getId().equals(jcFeeType.getId())) {
            throw new ServiceException("费用类型编码重复");
        }
        JcSysOrganization sysOrganization = jcSysOrganizationMapper.selectById(jcFeeType.getOrgId());
        if (sysOrganization != null) {
            jcFeeType.setOrgName(sysOrganization.getName());
        }
        jcFeeType.setModifyName(SecurityUtils.getUsername());
        jcFeeType.setModifyTime(DateUtils.getNowDate());
        return jcFeeTypeMapper.updateById(jcFeeType);
    }

    /**
     * 批量删除费用类型
     *
     * @param ids 需要删除的费用类型主键
     * @return 结果
     */
    @Override
    public int deleteJcFeeTypeByIds(Long[] ids) {
        return jcFeeTypeMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除费用类型信息
     *
     * @param id 费用类型主键
     * @return 结果
     */
    @Override
    public int deleteJcFeeTypeById(Long id) {
        return jcFeeTypeMapper.deleteById(id);
    }
}
